package com.myhome.controller;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.OSS;
import com.myhome.config.constant.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author QYiXin
 * @Date 2021 10 12 18 06
 **/
@RestController
@Api(tags = "文件上传控制器")
public class FileController {
    @Autowired
    private OSS ossClient;
    @Value("${oss.bucketName:qyixin-coin-exchange-images}")
    private String bucketName;
    @Value("${spring.cloud.alicloud.oss.endpoint}")
    private String endPoint ;
    @PostMapping("/image/AliYunImgUpload")
    @ApiOperation("文件上传")
    public R<String> fileUpload(MultipartFile file) throws IOException {
        String fileName = DateUtil.today().replaceAll("-", "/") + "/" + file.getOriginalFilename();
        ossClient.putObject(bucketName, fileName , file.getInputStream());
        return R.ok("https://" + bucketName + "." + endPoint + "/" + fileName);
    }
}
